<!--
 * @Description: 分包商月度考核待处理
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-03-07 10:25:38
 * @LastEditTime: 2023-05-05 09:28:38
 * @FilePath: \cscec-pms-admin\src\views\subcontractors\month\examinationProcessing.vue
-->

<template>
  <base-breadcrumb>
    <a-card>
      <title-name title="分包商月度考核" />
      <custom-form
        ref="form"
        :is-validate="true"
        :edit-model="''"
        :form="form"
        :form-list="formOption"
      />
      <a-table
        :row-key="'id'"
        :columns="columns"
        :data-source="data"
        :pagination="false"
        :loading="dataLoading"
        bordered
      >
        <template
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
            <a @click="handleDetail(record)">查看</a>
            <a v-if="!resolveInfo" @click="handleRevise(record)">修订分数</a>
          </MoreButton>
        </template>
      </a-table>
    </a-card>
    <RevisionScore
      :visible.sync="visible"
      :data="revisionScoreData"
    />
    <div class="btn_box page-btn-right-top">
      <a-button
        v-if="!resolveInfo"
        type="primary"
        :loading="submiting"
        @click="handleSubmit"
      >确认处理</a-button>
      <a-button
        class="ml20"
        @click="handleClose"
      >关闭</a-button>
    </div>
  </base-breadcrumb>
</template>
<script>
import { formOption, columns } from './model.js'
import RevisionScore from './components/RevisionScore'
import { listByBusiness, assessmentBatchConfirm } from '@/api/subcontractors/monthAssessment'
import { finishBusinessTodo } from '@/api/commons.js'
export default {
  name: 'examinationProcessing' + Date.now(),
  components: {
    RevisionScore
  },
  data() {
    return {
      // 表单项
      formOption,
      // 表单对象
      form: {},
      // 表头
      columns,
      // 查看、修订分数模态框显示隐藏
      visible: false,
      // 表格数据
      data: [],
      // 查看、修订分数模态框数据
      revisionScoreData: {
        type: 'detail',
        record: []
      },
      // 提交中
      submiting: false,
      //列表数据获取中
      dataLoading: false
    }
  },
  computed: {
    // 是否是业务待办查看处理信息
    resolveInfo: {
      get() {
        return this.$route.query?.resolveInfo === 'true'
      },
      set() {}
    }
  },
  mounted() {
    this.getDate()
  },
  methods: {
    /**
     * @description: 获取数据
     * @return {void}
     */
    getDate() {
      this.dataLoading = true
      listByBusiness({
        monthStr: decodeURIComponent(this.$route.query.noticeContent),
        projectId: this.$route.query.id
      })
        .then((res) => {
          if (res.code === this.SUCCESS_CODE) {
            this.data = this.resolveInfo ? res.data : res.data.map((el) => {
              el.revisedScore = null
              el.revisedLevel = null
              return el
            })
            this.form = {
              projectName: res.data[0]?.projectName,
              assessmentTimeName: res.data[0]?.assessmentTimeName
            }
          }
        })
        .finally(() => {
          this.dataLoading = false
        })
    },
    /**
     * @description: 查看
     * @param {object} record
     * @return {void}
     */
    handleDetail(record) {
      this.visible = true
      this.revisionScoreData = {
        type: 'detail',
        record
      }
    },
    /**
     * @description: 修订分数
     * @param {object} record
     * @return {void}
     */
    handleRevise(record) {
      this.visible = true
      this.revisionScoreData = {
        type: 'revise',
        record
      }
    },
    /**
     * @description: 提交
     * @return {void}
     */
    async handleSubmit() {
      this.submiting = true
      try {
        const res = await assessmentBatchConfirm(
          this.data
            .map(({ id, contentList, revisedLevel, revisedScore }) => {
              return {
                id,
                contentList,
                revisedLevel,
                revisedScore
              }
            })
        )
        const res1 = await finishBusinessTodo({
          id: this.$route.query.messageId
        })
        if (res.data.code === this.SUCCESS_CODE && res1.code === '0') {
          this.$message.success('处理成功')
          window.postMessage('finish', window.location.origin)
          this.$close()
        }
      } catch (error) {
        console.log(error)
      } finally {
        this.submiting = false
      }
    },
    /**
     * @description: 关闭
     * @return {void}
     */
    handleClose() {
      this.$close()
      // 刷新列表页
      window.postMessage('finish', window.location.origin)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
